﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FieldDevice.UI
{
    public partial class REF615Forms : DeviceFormBase
    {
        public REF615Forms()
        {
            InitializeComponent();
        }

        public REF615Forms(Device device)
            : base(device)
        {

            InitializeComponent();
            this.Text = "Device:" + device.GetType().Name + "(address:" + device.ModbusAddress + ")";
            this.ReadChangeEventHander =
            new Device.ReadChangeEventHander(device_ReadChangeEvent);
            device.ReadChangeEvent += this.ReadChangeEventHander;
            UpdateReadsDelegate = new VoidDelegate(this.UpdateReads);
        }

        void device_ReadChangeEvent(Device device, string name, float value)
        {
            if (this.InvokeRequired)
                this.Invoke(this.UpdateReadsDelegate);
            else
                this.UpdateReads();
        }

        public override void UpdateReads()
        {
            DetailList.Items.Clear();
            DetailList.Items.Add(Listvalue("A相电流", this.Device1.CurrentReads["Ia"].ToString("F2")));
            DetailList.Items.Add(Listvalue("B相电流", this.Device1.CurrentReads["Ib"].ToString("F2")));
            DetailList.Items.Add(Listvalue("C相电流", this.Device1.CurrentReads["Ic"].ToString("F2")));
            ProtectValue(this.Device1);
            SwitchValues(this.Device1);
        }

        private void button_Update_Click(object sender, EventArgs e)
        {
            this.Device1.DebugReads();
            this.UpdateReads();
        }
          public void ProtectValue(Device device)
        {
            if (device.CurrentReads["OverCurrentProtect"] != 0)
                DetailList.Items.Add(Listvalue("过流保护 开启/关闭", "开启"));
            else
                DetailList.Items.Add(Listvalue("过流保护 开启/关闭","关闭"));
            if (device.CurrentReads["ZeroFlowProtect"] != 0)
                DetailList.Items.Add(Listvalue("零流保护 开启/关闭", "开启"));
            else
                DetailList.Items.Add(Listvalue("零流保护 开启/关闭", "关闭"));

            if (device.CurrentReads["QuickBreak"] != 0)
                DetailList.Items.Add(Listvalue("速断 启动/停止", "启动"));
            else
                DetailList.Items.Add(Listvalue("速断 启动/停止", "停止"));
            if (device.CurrentReads["OvertemperatureTrip"] != 0)
                DetailList.Items.Add(Listvalue("超温跳闸 启动/停止", "启动"));
            else
                DetailList.Items.Add(Listvalue("超温跳闸 启动/停止", "停止"));
            
        }

          public void SwitchValues(Device device)
          {
                if (device.CurrentReads["Iso_Knife_Connect"] != 0 && device.CurrentReads["Iso_Knife_Cutoff"] == 0)
                      DetailList.Items.Add(Listvalue("隔离刀 分闸/合闸", "合闸"));
                  else
                    DetailList.Items.Add(Listvalue("隔离刀 分闸/合闸", "分闸"));
                if (device.CurrentReads["GroundedKnifeCutoff"] != 0 && device.CurrentReads["GroundedKnifeConnect"] == 0)
                    DetailList.Items.Add(Listvalue("接地刀 分闸/合闸", "分闸"));
                else
                    DetailList.Items.Add(Listvalue("接地刀 分闸/合闸", "合闸"));
                if (device.CurrentReads["CB_Connect"] != 0 && device.CurrentReads["CB_Cutoff"] == 0)
                    DetailList.Items.Add(Listvalue("断路器 分闸/合闸", "合闸"));
                else
                    DetailList.Items.Add(Listvalue("断路器 分闸/合闸", "分闸"));
                if (device.CurrentReads["Transformer_HighTemp"] != 0)
                    DetailList.Items.Add(Listvalue("变压器高温 是/否", "是"));
                else
                    DetailList.Items.Add(Listvalue("变压器高温 是/否", "否"));
                if (device.CurrentReads["Transformer_OvereTemp"] != 0)
                    DetailList.Items.Add(Listvalue("变压器超温 是/否", "是"));
                else
                    DetailList.Items.Add(Listvalue("变压器超温 是/否", "否"));

                if (device.CurrentReads["Vol_Tran_IsolationKnife_Connect"] != 0)
                    DetailList.Items.Add(Listvalue("引致变压柜的隔离刀 分闸/合闸", "合闸"));
                else
                    DetailList.Items.Add(Listvalue("引致变压柜的隔离刀 分闸/合闸", "分闸"));

                if (device.CurrentReads["PressureAnomaly"] != 0)
                    DetailList.Items.Add(Listvalue("压力异常 是/否", "是"));
                else
                    DetailList.Items.Add(Listvalue("压力异常 是/否", "否"));

                if (device.CurrentReads["LoadSwitch_Connect"] != 0)
                    DetailList.Items.Add(Listvalue("负荷开关合位 是/否", "是"));
                else
                    DetailList.Items.Add(Listvalue("负荷开关合位 是/否", "否"));
          }

        public ListViewItem Listvalue(string events,string value)
        {
            ListViewItem list = new ListViewItem(events);
            list.SubItems.Add(value);
            return list;
        }
    }
}
